﻿/// <reference path="P3.Core.SharePointClient.js" />
/// <reference path="P3.Core.Init.js" />
/// <reference path="P3.Core.Settings.js" />
/// <reference path="P3.Core.SPClientJQuery.js" />
/// <reference path="jquery.fabric.min.js" />
/// <reference path="JQuery/jquery-ui.min.js" />
/// <reference path="JQuery/jquery.min.js" />


/* DateTime functions */
Date.prototype.format = function (format) {
    var MMMM = ["\u0000", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var MMM = ["\u0001", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var dddd = ["\u0002", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var ddd = ["\u0003", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    function ii(i, len) { var s = i + ""; len = len || 2; while (s.length < len) s = "0" + s; return s; }

    var y = this.getFullYear();
    format = format.replace(/yyyy+/g, y);
    format = format.replace(/yy/g, y.toString().substr(2, 2));
    format = format.replace(/y/g, y);

    var M = this.getMonth() + 1;
    format = format.replace(/MMMM+/g, MMMM[0]);
    format = format.replace(/MMM/g, MMM[0]);
    format = format.replace(/MM/g, ii(M));
    format = format.replace(/M/g, M);

    var d = this.getDate();
    format = format.replace(/dddd+/g, dddd[0]);
    format = format.replace(/ddd/g, ddd[0]);
    format = format.replace(/dd/g, ii(d));
    format = format.replace(/d/g, d);

    var H = this.getHours();
    format = format.replace(/HH+/g, ii(H));
    format = format.replace(/H/g, H);

    var h = H > 12 ? H - 12 : H == 0 ? 12 : H;
    format = format.replace(/hh+/g, ii(h));
    format = format.replace(/h/g, h);

    var m = this.getMinutes();
    format = format.replace(/mm+/g, ii(m));
    format = format.replace(/m/g, m);

    var s = this.getSeconds();
    format = format.replace(/ss+/g, ii(s));
    format = format.replace(/s/g, s);

    var f = this.getMilliseconds();
    format = format.replace(/fff+/g, ii(f, 3));
    f = Math.round(f / 10);
    format = format.replace(/ff/g, ii(f));
    f = Math.round(f / 10);
    format = format.replace(/f/g, f);

    var T = H < 12 ? "AM" : "PM";
    format = format.replace(/TT+/g, T);
    format = format.replace(/T/g, T.charAt(0));

    var t = T.toLowerCase();
    format = format.replace(/tt+/g, t);
    format = format.replace(/t/g, t.charAt(0));

    var day = this.getDay() + 1;
    format = format.replace(new RegExp(dddd[0], "g"), dddd[day]);
    format = format.replace(new RegExp(ddd[0], "g"), ddd[day]);

    format = format.replace(new RegExp(MMMM[0], "g"), MMMM[M]);
    format = format.replace(new RegExp(MMM[0], "g"), MMM[M]);

    return format;
};



/* Url Functions */

function getQuerystring(key, default_) {
    if (default_ == null) {
        default_ = "";
    }
    var search = unescape(location.search);
    if (search == "") {
        return default_;
    }
    search = search.substr(1);
    var params = search.split("&");
    for (var i = 0; i < params.length; i++) {
        var pairs = params[i].split("=");
        if (pairs[0] == key) {
            return pairs[1];
        }
    }
    return default_;
}

function getUrlParts(url) {
    var a = document.createElement('a');
    a.href = url;

    return {
        href: a.href,
        host: a.host,
        hostname: a.hostname,
        port: a.port,
        pathname: a.pathname,
        protocol: a.protocol,
        hash: a.hash,
        search: a.search
    };
}

// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresearch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// And thanks to everyone else who has provided comments and suggestions.
// ====================================================================
function URLEncode(txt, oldBrowser) {
    var plaintext = txt;
    if (oldBrowser) {  // OLD Browser mode
        // The Javascript escape and unescape functions do not correspond
        // with what browsers actually do...
        var SAFECHARS = "0123456789" + 				// Numeric
		        "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
		        "abcdefghijklmnopqrstuvwxyz" +
		        "-_.!~*'()"; 				// RFC2396 Mark characters
        var HEX = "0123456789ABCDEF";

        var encoded = "";
        for (var i = 0; i < plaintext.length; i++) {
            var ch = plaintext.charAt(i);
            if (ch == " ") {
                encoded += "+"; 			// x-www-urlencoded, rather than %20
            } else if (SAFECHARS.indexOf(ch) != -1) {
                encoded += ch;
            } else {
                var charCode = ch.charCodeAt(0);
                if (charCode > 255) {
                    alert("Unicode Character '"
                          + ch
                          + "' cannot be encoded using standard RFC2396 encoding.\n" +
	                  "(URL encoding only supports 8-bit characters.)\n" +
			          "A space (+) will be substituted.");
                    encoded += "+";
                } else {
                    encoded += "%";
                    encoded += HEX.charAt((charCode >> 4) & 0xF);
                    encoded += HEX.charAt(charCode & 0xF);
                }
            }
        } // for
        return encoded;
    } else {  // Modern browser mode
        return encodeURIComponent(plaintext);
    }
    return false;
}

function URLDecode(txt, oldBrowser) {
    var encoded = txt;
    if (oldBrowser) {  // OLD Browser mode
        // Replace + with ' '
        // Replace %xx with equivalent character
        // Put [ERROR] in output if %xx is invalid.
        var HEXCHARS = "0123456789ABCDEFabcdef";
        var plaintext = "";
        var i = 0;
        while (i < encoded.length) {
            var ch = encoded.charAt(i);
            if (ch == "+") {
                plaintext += " ";
                i++;
            } else if (ch == "%") {
                if (i < (encoded.length - 2)
					  && HEXCHARS.indexOf(encoded.charAt(i + 1)) != -1
					  && HEXCHARS.indexOf(encoded.charAt(i + 2)) != -1) {
                    plaintext += unescape(encoded.substr(i, 3));
                    i += 3;
                } else {
                    alert('Bad escape combination near ...' + encoded.substr(i));
                    plaintext += "%[ERROR]";
                    i++;
                }
            } else {
                plaintext += ch;
                i++;
            }
        } // while
        return plaintext;
    } else { // Modern browser mode
        try {
            return decodeURIComponent(encoded);
        } catch (error) {
            throw new Error(error + ' - Probably the second byte of a Unicode character is missing.');
        }
    }
    return false;
}

function safeLogger(message) {
    if (window.console != null && typeof (window.console) != typeof (undefined)) {
        console.log(message);
    }
}

//sets the select tag value
function setSelectedOption(select, value) {
    var opts = select.options;
    var l = opts.length;
    if (select == null) return;
    for (var i = 0; i < l; i++) {
        if (opts[i].value == value) {
            select.selectedIndex = i;
            return true;
        }
    }
    for (var i = 0; i < l; i++) {
        if (opts[i].text == value) {
            select.selectedIndex = i;
            return true;
        }
    }
    return false;
}

// Gets Url without the last dash
function getUrlWithoutDash(url) {
    if ((url != null) && (url.length > 0)) {
        if (url[url.length - 1] == "/")
            return url.slice(0, -1);
    }
    else return "";
}